#if !SILVERLIGHT
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.IO;

namespace SBPweb.Common.Messaging
{
	/// <summary>
	/// Class of MessageHandler. Sends E-Mails.
	/// </summary>
	public class MessageHandler
	{
		#region Constructor logic

		/// <summary>
		/// Contructor of MessageHandler
		/// </summary>
		public MessageHandler()
		{
		}

		#endregion

		#region Public members

		#endregion

		#region Private helper methods

		private static MailAddressCollection GetAddressCollection(List<string> stringList)
		{
			MailAddressCollection mac = new MailAddressCollection();

			foreach (string address in stringList)
			{
				mac.Add(address);
			}

			return mac;
		}

		#endregion

		#region Public helpers methods

		/// <summary>
		/// Send message
		/// </summary>
		/// <param name="smtpHost">smtp host</param>
		/// <param name="sender">sender</param>
		/// <param name="recipients">recipients</param>
		/// <param name="subject">subject</param>
		/// <param name="body">body</param>
		public static void SendMessage(string smtpHost, MailAddress sender, MailAddressCollection recipients, string subject, string body)
		{
			MailMessage mm = new MailMessage(sender, new MailAddress(recipients.ToString()));
			mm.Subject = subject;
			mm.Body = body;
            mm.IsBodyHtml = true;
            SmtpClient sc = new SmtpClient(smtpHost);
			sc.Send(mm);
		}

		/// <summary>
		/// Send message
		/// </summary>
		/// <param name="smtpHost">smtp host</param>
		/// <param name="userName">username</param>
		/// <param name="password">password</param>
		/// <param name="sender">sender</param>
		/// <param name="recipients">recipients</param>
		/// <param name="subject">subject</param>
		/// <param name="body">body</param>
        public static void SendMessage(string smtpHost, string userName, string password, MailAddress sender, MailAddressCollection recipients, string subject, string body)
        {
            MailMessage mm = new MailMessage(sender, new MailAddress(recipients.ToString()));

            mm.Subject = subject;
            mm.Body = body;
            mm.IsBodyHtml = true;
            SmtpClient sc = new SmtpClient(smtpHost);
            sc.Credentials = new System.Net.NetworkCredential(userName, password);
            sc.UseDefaultCredentials = false;
            sc.Send(mm);
        }
        
		/// <summary>
		/// Send message
		/// </summary>
		/// <param name="smtpHost">smtp host</param>
		/// <param name="sender">sender</param>
		/// <param name="recipients">recipients</param>
		/// <param name="subject">subject</param>
		/// <param name="body">body</param>
        public static void SendMessage(string smtpHost, string sender, List<string> recipients, string subject, string body)
		{
			MailAddress from = new MailAddress(sender);
			MailAddressCollection to = GetAddressCollection(recipients); ;
			SendMessage(smtpHost, from, to, subject, body);
		}

		/// <summary>
		/// Send message
		/// </summary>
		/// <param name="smtpHost">smtp host</param>
		/// <param name="sender">sender</param>
		/// <param name="recipients">recipients</param>
		/// <param name="carbonCopies">cc</param>
		/// <param name="subject">subject</param>
		/// <param name="body">body</param>
		public static void SendMessage(string smtpHost, string sender, List<string> recipients, List<string> carbonCopies, string subject, string body)
		{
			//MailAddress from = new MailAddress(sender);
			
			MailAddressCollection to = GetAddressCollection(recipients);
			MailAddressCollection ccs = GetAddressCollection(carbonCopies);

			MailMessage mm = new MailMessage(new MailAddress(sender), new MailAddress(to.ToString()));
			mm.CC.Add(ccs.ToString());
			mm.Subject = subject;
			mm.Body = body;
            mm.IsBodyHtml = true;

			SmtpClient sc = new SmtpClient(smtpHost);
			sc.Send(mm);
		}

		/// <summary>
		/// Send message
		/// </summary>
		/// <param name="smtpHost">smtp host</param>
		/// <param name="sender">sender</param>
		/// <param name="recipients">recipients</param>
		/// <param name="carbonCopies">cc</param>
		/// <param name="blindCarbonCopies">bcc</param>
		/// <param name="subject">subject</param>
		/// <param name="body">body</param>
		public static void SendMessage(string smtpHost, string sender, List<string> recipients, List<string> carbonCopies, List<string> blindCarbonCopies, string subject, string body)
		{
			//MailAddress from = new MailAddress(sender);

			MailAddressCollection to = GetAddressCollection(recipients);
			MailAddressCollection ccs = GetAddressCollection(carbonCopies);
			MailAddressCollection bccs = GetAddressCollection(blindCarbonCopies);

			MailMessage mm = new MailMessage(new MailAddress(sender), new MailAddress(to.ToString()));
			mm.CC.Add(ccs.ToString());
			mm.Bcc.Add(bccs.ToString());
			mm.Subject = subject;
			mm.Body = body;
            mm.IsBodyHtml = true;

			SmtpClient sc = new SmtpClient(smtpHost);
			sc.Send(mm);
		}

		/// <summary>
		/// Send message
		/// </summary>
		/// <param name="smtpHost">smtp host</param>
		/// <param name="sender">sender</param>
		/// <param name="recipients">recipients</param>
		/// <param name="carbonCopies">cc</param>
		/// <param name="blindCarbonCopies">bcc</param>
		/// <param name="subject">subject</param>
		/// <param name="body">body</param>
		/// <param name="priority">priority</param>
		public static void SendMessage(string smtpHost, string sender, List<string> recipients, List<string> carbonCopies, List<string> blindCarbonCopies, string subject, string body, MailPriority priority)
		{
			//MailAddress from = new MailAddress(sender);

			MailAddressCollection to = GetAddressCollection(recipients);
			MailAddressCollection ccs = GetAddressCollection(carbonCopies);
			MailAddressCollection bccs = GetAddressCollection(blindCarbonCopies);

			MailMessage mm = new MailMessage(new MailAddress(sender), new MailAddress(to.ToString()));
			mm.CC.Add(ccs.ToString());
			mm.Bcc.Add(bccs.ToString());
			mm.Subject = subject;
			mm.Body = body;
            mm.IsBodyHtml = true;
            mm.Priority = priority;

			SmtpClient sc = new SmtpClient(smtpHost);
			sc.Send(mm);
		}

		/// <summary>
		/// Send message
		/// </summary>
		/// <param name="smtpHost">smtp host</param>
		/// <param name="sender">sender</param>
		/// <param name="recipients">recipients</param>
		/// <param name="carbonCopies">cc</param>
		/// <param name="blindCarbonCopies">bcc</param>
		/// <param name="subject">subject</param>
		/// <param name="body">body</param>
		/// <param name="priority">priority</param>
		/// <param name="attachments">attachments</param>
		public static void SendMessage(string smtpHost, string sender, List<string> recipients, List<string> carbonCopies, List<string> blindCarbonCopies, string subject, string body, MailPriority priority, Dictionary<string, Stream> attachments)
		{
			//MailAddress from = new MailAddress(sender);

			MailAddressCollection to = GetAddressCollection(recipients);
			MailAddressCollection ccs = GetAddressCollection(carbonCopies);
			MailAddressCollection bccs = GetAddressCollection(blindCarbonCopies);

			MailMessage mm = new MailMessage(new MailAddress(sender), new MailAddress(to.ToString()));
			mm.CC.Add(ccs.ToString());
			mm.Bcc.Add(bccs.ToString());
			mm.Subject = subject;
			mm.Body = body;
            mm.IsBodyHtml = true;
            mm.Priority = priority;

			foreach (KeyValuePair<string, Stream> kvp in attachments)
			{
				mm.Attachments.Add(new Attachment(kvp.Value, kvp.Key));
			}

			SmtpClient sc = new SmtpClient(smtpHost);
			sc.Send(mm);
		}

		#endregion
	}
}
#endif